package net.oschina.git.fsppp.view.model;

import net.oschina.git.fsppp.manager.DirManager;
import net.oschina.git.fsppp.manager.FileManager;
import net.oschina.git.fsppp.manager.IDirManager;
import net.oschina.git.fsppp.manager.IFileManager;
import net.oschina.git.fsppp.model.P3Dir;
import net.oschina.git.fsppp.model.P3FAT;
import net.oschina.git.fsppp.model.SimpleResponse;
import net.oschina.git.fsppp.utils.P3Utils;
import net.oschina.git.fsppp.view.contract.FileOPContract;
import rx.Observable;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;


/**
 * Created by QiHuang on 2016/10/7.
 */
public class FileModel implements FileOPContract.Model {
    private IDirManager dirManager = DirManager.getInstance();
    private FileManager fileManager = FileManager.getInstance();

    @Override
    public Observable<SimpleResponse<List<P3Dir>>> getInitialDisk() {
        SimpleResponse<List<P3Dir>> response = fileManager.listDisk();
        return Observable.just(response);
    }

    @Override
    public Observable<SimpleResponse<List<P3Dir>>> getCurrentFile(String dirPath) {
        SimpleResponse<List<P3Dir>> response = dirManager.listByPath(dirPath);
        return Observable.just(response);
    }

    @Override
    public Observable<SimpleResponse<P3Dir>> createNewDir(String dirPath, String name) {
        SimpleResponse<P3Dir> response = dirManager.createDir(dirPath, name);
        return Observable.just(response);
    }

    @Override
    public Observable<SimpleResponse<P3Dir>> createNewFile(String path, String name) {
        SimpleResponse<P3Dir> response = fileManager.createFile(path, name);
        return Observable.just(response);
    }

    @Override
    public Observable<SimpleResponse<List<P3Dir>>> getChildDirs(P3Dir parent) {
        SimpleResponse<List<P3Dir>> response = dirManager.listDir(parent);
        return Observable.just(response);
    }

    @Override
    public Observable<P3FAT> getFatByDisk(String diskName) {
        try {
            return Observable.just(P3Utils.getFatByDisk(diskName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Observable<SimpleResponse<P3Dir>> editFile(P3Dir dir) {
        SimpleResponse<P3Dir> response = fileManager.editFile(dir);
        return Observable.just(response);
    }

    @Override
    public Observable<SimpleResponse<P3Dir>> deleteFile(P3Dir dir) {
        SimpleResponse<P3Dir> response = fileManager.deleteFile(dir);
        return Observable.just(response);
    }

    @Override
    public Observable<SimpleResponse> formatDisk(String diskName) {
        return Observable.just(fileManager.initDisk(diskName));
    }

    @Override
    public Observable<LinkedList<Integer>> getFileLink(P3Dir dir) {
        return Observable.just(P3Utils.getFileLinks(dir));
    }

    @Override
    public Observable<SimpleResponse<List<P3Dir>>> searchFile(String path, String filename) {
        SimpleResponse<List<P3Dir>> response = dirManager.search(path, filename);
        return Observable.just(response);
    }


}
